<?php
// capcha.php: gets the CAPTCHAs from the DB from the first sweep

define(ERR_DB, 'ERROR: Cannot open database connection. (check your configs)');
define(ERR_DBQUERY, 'ERROR: Cannot query database. (check your configs)');
if(isset($_REQUEST['page'])) $prepage = $_REQUEST['page'];
else $prepage = 1;
if(isset($_REQUEST['num'])) $prenum = $_REQUEST['num'];
else $prenum = 1;
if(isset($_REQUEST['id'])) $id = $_REQUEST['id'];
require_once 'config.php';
require_once 'snarf.lib.php';
$dbh = mysql_connect($mysqldb['host'], $mysqldb['user'], $mysqldb['pass']);
if(!$dbh) die(ERR_DB);
mysql_select_db($mysqldb['db'], $dbh);

if(isset($_REQUEST['del'])) {
	$sql = sprintf("DELETE FROM capcha WHERE id = '%s';",$_REQUEST['del']);
	mysql_query($sql, $dbh) || die(ERR_DBQUERY);
}

$sql = 'SELECT count(*) FROM capcha;';
$result = mysql_query($sql, $dbh);
if(!$result) die(ERR_DBQUERY);
if(mysql_result($result, 0) == 0) {
	echo 'Sorry, there are no CAPTCHAs to process.<br>';
	echo '<br><a href="signmeup.php">Main Menu</a>';
	die;
}
$sql = 'SELECT * FROM capcha LIMIT 1;';
$result = mysql_query($sql, $dbh);
if(!$result) die(ERR_DBQUERY);
$rows = mysql_num_rows($result);
if(isset($_POST['cap'])) {
	$post = mysql_result($result, 0, 'post');
	$url = mysql_result($result, 0, 'url');
	$user = mysql_result($result, 0, 'user');
	$pass = mysql_result($result, 0, 'pass');
	$host = mysql_result($result, 0, 'host');
	$rank = mysql_result($result, 0, 'googlerank');
	$id = mysql_result($result, 0, 'id');

	$ph = curl_init($url);
	$cap = sprintf('%s',$_POST['cap']);
	$post = 'image_verification='.$cap."&".$post;
	curl_setopt($ph, CURLOPT_COOKIEFILE, $cookie);
	curl_setopt($ph, CURLOPT_POSTFIELDS, $post);
	curl_setopt($ph, CURLOPT_RETURNTRANSFER, 1);
	$regpage = curl_exec($ph);
	//echo $regpage;
		if(preg_match('/'.$query['succ'].'/i', $regpage) || preg_match('/name already exists/i', $regpage)) {
			$sql = sprintf("INSERT INTO signmeup(url,user,pass,googlerank) VALUES('%s', '%s', '%s', '%s');", $host, $user, $pass, $rank);
			mysql_query($sql, $dbh) || die(ERR_DBQUERY);
			$sql = sprintf("DELETE FROM capcha WHERE id = '%s';",$id);
			mysql_query($sql, $dbh) || die(ERR_DBQUERY);
		}
	curl_close($ph);
}
$sql = 'SELECT * FROM capcha LIMIT 1;';
$result = mysql_query($sql, $dbh);
if(!$result) die(ERR_DBQUERY);
$rows = mysql_num_rows($result);


$del = mysql_result($result, 0, 'id');
$post = mysql_result($result, 0, 'post');
$url = mysql_result($result, 0, 'url');
$user = mysql_result($result, 0, 'user');
$pass = mysql_result($result, 0, 'pass');
$host = mysql_result($result, 0, 'host');
echo "CAPTCHA: ".$url.'<br>';
echo '<img src="get_cap.php?host='.str_replace('register', '', $host).'">';
echo '<form action="?run=yes" method="POST"><br>Enter CAPTCHA: <input type="text" name="cap">
<input type="hidden" value="id" value="'.$id.'"></form>';
echo '<br><a href="capcha.php?del='.$del.'">Skip(delete from DB)</a>';

?>
